package com.aboo.service.impl;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.aboo.dao.AdminUserDao;
import com.aboo.dao.RRoleUserDao;
import com.aboo.dao.SysAdminLogDao;
import com.aboo.po.AdminUserPO;
import com.aboo.po.MenuPO;
import com.aboo.po.Page;
import com.aboo.po.RRoleUserPO;
import com.aboo.po.RolePO;
import com.aboo.po.SysAdminLogPO;
import com.aboo.pub.exception.BasicException;
import com.aboo.pub.result.ResultConstant;
import com.aboo.pub.result.ResultInfo;
import com.aboo.service.AdminUserService;
import com.aboo.util.DateUtil;
import com.aboo.util.EncryptionUtil;

public class AdminUserServiceImpl implements AdminUserService {
	
	private AdminUserDao adminUserDao;
	private SysAdminLogDao sysAdminLogDao;
	private RRoleUserDao rRoleUserDao;

	public void login(ResultInfo ri, HttpServletRequest request, String loginName, String pwd) throws BasicException {
		try {
			AdminUserPO param = new AdminUserPO();
			param.setLoginName(loginName);
			param.setPassword(EncryptionUtil.encodeMD5(pwd));
			AdminUserPO result = adminUserDao.validateAdminUser(param);
			
			if (result != null) {
				if (result.getSigninTimes() == null) {
					result.setSigninTimes(new Integer(1));
				} else {
					result.setSigninTimes(new Integer(result.getSigninTimes().intValue() + 1));
				}
				result.setLastSigninTime(DateUtil.getChar14());
				adminUserDao.update(result);
				
				ri.setResultCode(ResultConstant.STATUS_OK);
				ri.setObj(result);
				
				SysAdminLogPO log = new SysAdminLogPO();
				log.setUserId(result.getId());
				log.setOpTime(DateUtil.getChar14());
				//用户登陆
				log.setOpType("1");
				sysAdminLogDao.save(log);
			} else {
				ri.setResultCode(ResultConstant.STATUS_ERROR);
			}	
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("用户登陆时出错！", ex);
		}
	}

	public void saveAdminUser(ResultInfo ri, AdminUserPO param)
			throws BasicException {
		if (StringUtils.isEmpty(param.getLoginName())) {
			ri.setResultCode(ResultConstant.STATUS_ERROR);
			return;
		}
		
		try {
			//新增用户时，自动设置默认密码
			param.setPassword(EncryptionUtil.encodeMD5("111111"));
			adminUserDao.save(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("保存用户信息出错！", ex);
		}
	}

	public void deleteAdminUser(ResultInfo ri, List<String> userIds) throws BasicException {
		try {
			if (userIds != null) {
				String userId = null;
				for (Iterator<String> iter = userIds.iterator(); iter.hasNext();) {
					userId = iter.next();
					if ("1".equals(userId)) {
						throw new BasicException("管理员账户不允许删除！");
					}
					adminUserDao.delete(new Integer(userId));
				}
			}
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("删除用户信息出错！", ex);
		}
	}

	public void updateAdminUser(ResultInfo ri, AdminUserPO param)
			throws BasicException {
		if (param.getId() == null) {
			ri.setResultCode(ResultConstant.STATUS_ERROR);
			return;
		}
		
		try {
			if (param.getPassword() != null) {
				param.setPassword(EncryptionUtil.encodeMD5(param.getPassword()));
			}
			adminUserDao.update(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("修改用户信息出错！", ex);
		}
	}

	public void findUserList(ResultInfo ri, AdminUserPO param, Page page)
			throws BasicException {
		try {
			if (page != null) {
				Integer count = adminUserDao.findCount(param);
				page.setTotalRecordSize(count);
			}
			
			List<AdminUserPO> result = adminUserDao.findList(param, page);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询用户列表信息出错！", ex);
		}
	}
	
	public void findUserCount(ResultInfo ri, AdminUserPO param) throws BasicException {
		try {
			Integer result = adminUserDao.findCount(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询用户数量出错！", ex);
		}
	}
	
	public void findUser(ResultInfo ri, AdminUserPO param) throws BasicException {
		try {
			AdminUserPO result = adminUserDao.find(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询用户信息出错！", ex);
		}
	}
	
	public void findUserMenuList(ResultInfo ri, Integer userId) throws BasicException {
		try {
			List<MenuPO> result = adminUserDao.findMenuById(userId);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询用户的资源信息出错！", ex);
		}
	}
	
	public void findUserRoleList(ResultInfo ri, Integer userId) throws BasicException {
		try {
			List<RolePO> result = adminUserDao.findRoleById(userId);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询用户的角色信息出错！", ex);
		}
	}
	
	public void saveUserRoleList(ResultInfo ri, Integer userId,	List<String> roleIds) throws BasicException {
		try {
			RRoleUserPO po = new RRoleUserPO();
			po.setUserId(userId);
			rRoleUserDao.delete(po);
			
			if (roleIds != null) {
				for (String roleId : roleIds) {
					po = new RRoleUserPO();
					po.setUserId(userId);
					po.setRoleId(new Integer(roleId));
					rRoleUserDao.save(po);
				}
			}	
			
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("保存用户的角色信息出错！", ex);
		}
	}

	public AdminUserDao getAdminUserDao() {
		return adminUserDao;
	}
	public void setAdminUserDao(AdminUserDao adminUserDao) {
		this.adminUserDao = adminUserDao;
	}
	public SysAdminLogDao getSysAdminLogDao() {
		return sysAdminLogDao;
	}
	public void setSysAdminLogDao(SysAdminLogDao sysAdminLogDao) {
		this.sysAdminLogDao = sysAdminLogDao;
	}
	public RRoleUserDao getrRoleUserDao() {
		return rRoleUserDao;
	}
	public void setrRoleUserDao(RRoleUserDao rRoleUserDao) {
		this.rRoleUserDao = rRoleUserDao;
	}

}
